//今日推荐商品

function getGoodsType(page, size, type) {
	$.ajax({
		url: 'http://localhost:3000/admin/goodList',
		method: 'GET',
		async: false,
		data: { page: page, size: size, type: type },
		success: function (data) {
			$('.m_today_center')[0].innerHTML = '';
			data.data.dataList.forEach(function (today, index) {
				$('.m_today_center')[0].innerHTML += `    <div class="goods-box2 ">
        <div class="goods clearfix">
          <a href="javascript:void(0)"onclick="datai(${today.id})">
            <div class="img-box">
              <img src="http://localhost:3000/update/${today.cover}">
            </div>
          </a>
          <div class="goods-right">
            <a href="javascript:void(0)"onclick="datai(${today.id})">
              <p class="goodsname">${today.name}</p>
            </a>
            <p class="goods-descript">${today.intro}</p>
            <p class="goods-spec">${today.spec}</p>
            <p class="goodsprices"><span class="yj">&yen;<span class="yjcont">${today.price}</span></span></p>
            <p class="addcart" data-id="${today.id}"><i class="iconfont icon-gouwuche3"></i>加入购物车</p>
          </div>
        </div>
      </div>`;
			});
		}
	});
}
getGoodsType(1, 5, 1);
//热销
function getHot(page, size) {
	$.ajax({
		url: 'http://localhost:3000/admin/hotList',
		method: 'GET',
		async: false,
		data: { page: page, size: size },
		success: function (data) {
			$('.m_hot_two')[0].innerHTML = '';
			$('.m_hot_one')[0].innerHTML = '';
			data.data.dataList.forEach(function (hot, index) {
				console.log(hot.id);
				if (index >= 8) {
					return;
				}
				if (index < 3) {
					$('.m_hot_one')[0].innerHTML += `
          <div class="goods-box1 ">
          <div class="goods clearfix">
            <div class="m-pm" style="background-image: url('./static/index/img/m_top_${index + 1}.png')"></div>
            <a onclick="datai(${hot.id})" href="javascript:void(0)">
              <div class="img-box">
                <img src="http://localhost:3000/update/${hot.cover}">
              </div>
            </a>
            <div class="goods-right">
              <a href="javascript:void(0)" onclick="datai(${hot.id}">
                <p class="goodsname">${hot.name}</p>
              </a>
              <p class="goods-descript">${hot.intro}</p>
              <p class="goods-spec">${hot.spec}/盒</p>
              <p class="goodsprices"><span class="yj">&yen;<span class="yjcont">${hot.price}</span></span></p>
              <p class="addcart" data-id="${hot.id}"><i class="iconfont icon-gouwuche3"></i>加入购物车</p>
            </div>
          </div>
        </div>
      `;
				} else {
					$('.m_hot_one')[0].innerHTML += `
          <div class="goods-box ">
          <div class="goods clearfix">
            <div class="m-pm"><span></span></div>
            <a href="javascript:void(0)" onclick="datai(${hot.id})">
              <div class="img-box">
                <img src="${hot.cover}">
              </div>
            </a>
            <div class="goods-right">
              <a href="javascript:void(0)"onclick="datai(${hot.id})">
                <p class="goodsname">${hot.name}</p>
              </a>
              <p class="goods-descript">${hot.intro}</p>
              <p class="goods-spec">${hot.spec}</p>
              <p class="goodsprices"><span class="yj">&yen;<span class="yjcont">${hot.price}</span></span></p>
              <p class="addcart" data-id="${hot.id}"><i class="iconfont icon-gouwuche3"></i>加入购物车</p>
            </div>
          </div>
        </div>         
          `;
				}
			});
		}
	});
}
getHot(1, 10);
init();
getTypegoods(1, 100);

//产品分类
function getTypegoods(page, size, typeid) {
	$.ajax({
		url: 'http://localhost:3000/admin/queryGoodsByType',
		method: 'GET',
		async: false,
		data: { page: page, size: size, typeId: typeid },
		success: function (data) {
			$('.goodstype')[0].innerHTML = '';
			for (var i = 0; i < data.data.length; i++) {
				$('.goodstype')[0].innerHTML += `
        <div class="main Louti vegetable">
        <div class="m-hot-tit clearfix">
          <div class="m-hot-tit-left"><span>${data.data[i].name}</span></div>
          <a href="javascript:void(0)">
            <div onclick="findGoodsByType(${data.data[i].list[i].type_id},'${data.data[i].name}')" class="m-hot-tit-right">查看更多 ></div>
          </a>
        </div>
        <div class="main-box">
          <div class="m-swiper-container">
            <div class="swiper-wrapper">
            
            </div>
            <div class="swiper-pagination"></div>
          </div>
        </div>
      </div>      
        `;
				var div = document.createElement('div');
				div.className = 'swiper-slide';
				document.querySelectorAll('.swiper-wrapper')[i].appendChild(div);
				for (var j = 0; j < data.data[i].list.length; j++) {
					if (j % 5 == 0) {
						div = document.createElement('div');
						div.className = 'swiper-slide';
						document.querySelectorAll('.swiper-wrapper')[i].appendChild(div);
					}
					div.innerHTML += `
          <div class="goods-box ">
          <div class="goods">
            <a href="javascript:void(0)" onclick="datai(${data.data[i].list[j].id})">
              <div class="img-box">
                <img src="http://localhost:3000/update/${data.data[i].list[j].cover}">
              </div>
            </a>
            <a href="javascript:void(0)" onclick="datai(${data.data[i].list[j].id})">
              <p class="goodsname">${data.data[i].list[j].name}</p>
            </a>
            <p class="goods-descript">${data.data[i].list[j].intro}</p>
            <p class="goods-spec">${data.data[i].list[j].spec}</p>
            <p class="goodsprices"><span class="yj">&yen;<span class="yjcont">${data.data[i].list[j].price}</span></del></p>
            <p class="addcart" data-id="${data.data[i].list[j].id}"><i class="iconfont icon-gouwuche3"></i>加入购物车</p>
          </div>
        </div>         
          `;
				}
			}
		}
	});
	new Swiper('.m-swiper-container', {
		loop: true,
		autoplay: {
			delay: 1800,
			disableOnInteraction: false
		},
		speed: 1800,
		pagination: {
			el: '.swiper-pagination',
			clickable: true
		}
	});
}

function init() {
	$('.goods-box').on('mouseenter', function () {
		$(this).addClass('active');
		$(this).find('.addcart').show();
		$(this).find('.reserve').show();
		$(this).find('.m_stop').show();
		$(this).find('.goodsprices').hide();
	});
	$('.goods-box').on('mouseleave', function () {
		$(this).removeClass('active');
		$(this).find('.addcart').hide();
		$(this).find('.reserve').hide();
		$(this).find('.m_stop').hide();
		$(this).find('.goodsprices').show();
	});
	$('.goods-box1').on('mouseenter', function () {
		$(this).addClass('active');
	});
	$('.goods-box1').on('mouseleave', function () {
		$(this).removeClass('active');
	});
	$('.goods-box2').on('mouseenter', function () {
		$(this).addClass('active');
		$(this).find('.addcart').show();
		$(this).find('.reserve').show();
		$(this).find('.goodsprices').hide();
	});
	$('.goods-box2').on('mouseleave', function () {
		$(this).removeClass('active');
		$(this).find('.addcart').hide();
		$(this).find('.reserve').hide();
		$(this).find('.goodsprices').show();
	});
}
